﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormDoiTac : DevComponents.DotNetBar.RibbonForm
    {
        public FormDoiTac()
        {
            InitializeComponent();
        }
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        private int idtam = 0;
        private void FormDoiTac_Load(object sender, EventArgs e)
        {
            GetListDoiTac();
            GetListNhomDoiTac();
            Reset();
        }

        private void bttThemMoi_Click(object sender, EventArgs e)
        {
            var t = new FormDoiTacThem();
            t.ShowDialog();
        }

        private void bttChinhSua_Click(object sender, EventArgs e)
        {
            var t = new FormDoiTacCapNhap();
            t.id = idtam;
            t.ShowDialog();
        }

        private void bttXoa_Click(object sender, EventArgs e)
        {
            Delete();
            GetListDoiTac();
        }

        private void bttLamMoi_Click(object sender, EventArgs e)
        {
            GetListDoiTac();
            GetListNhomDoiTac();
            Reset();
        }

        private void bttTimKiem_Click(object sender, EventArgs e)
        {
            TimKiem();
            Reset();
        }

       
        private void dgvDoiTac_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvDoiTac.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);

            KiemTra();
        }
        private void dgvDoiTac_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var t = new FormDoiTacCapNhap();
            t.id = idtam;
            t.ShowDialog();
        }
        void GetListDoiTac()
        {
            dgvDoiTac.DataSource = db.DoiTacs.ToList();

            idtam = 0;
            var n = dgvDoiTac.Columns["NhomDoiTacId"] as DataGridViewComboBoxColumn;
            n.DataSource = db.NhomDoiTacs.ToList();
            n.DisplayMember = "TenNhomDoiTac";
            n.ValueMember = "Id";
        }

        void GetListNhomDoiTac()
        {
            cbbNhomDoiTac.DataSource = db.NhomDoiTacs.ToList();
            cbbNhomDoiTac.DisplayMember = "TenNhomDoiTac";
            cbbNhomDoiTac.ValueMember = "Id";
            cbbNhomDoiTac.DropDownColumns = "Id,TenNhomDoiTac";
            cbbNhomDoiTac.DropDownColumnsHeaders = "Mã nhóm\r\nTên nhóm đối tác\r\n";
        }
        void Delete()
        {
            if (idtam != 0)
            {
                var t = db.DoiTacs.Find(idtam);
                if (
                    MessageBox.Show("Bạn muốn xóa đối tác: " + t.TenDoiTac + "!", "Xác nhận", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        db.DoiTacs.Remove(t);
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }

                }
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa!");
            }
        }

        void TimKiem()
        {
            //var idnhomdoitac=
            if (txtTuKhoa.Text != "" && Convert.ToInt32(cbbNhomDoiTac.SelectedValue) != 0)
            {
                dgvDoiTac.DataSource = db.DoiTacs.Where(t => t.NhomDoiTacId == Convert.ToInt32(cbbNhomDoiTac.SelectedValue) &&
                            t.TenDoiTac.Contains(txtTuKhoa.Text)).ToList();
            }
            else if (txtTuKhoa.Text != "" && Convert.ToInt32(cbbNhomDoiTac.SelectedValue) == 0)
            {
                dgvDoiTac.DataSource =db.DoiTacs.Where(t =>t.TenDoiTac.Contains(txtTuKhoa.Text)).ToList();
            }
            else if (txtTuKhoa.Text == "" && Convert.ToInt32(cbbNhomDoiTac.SelectedValue) != 0)
            {
                dgvDoiTac.DataSource = db.DoiTacs.Where(t => t.NhomDoiTacId == Convert.ToInt32(cbbNhomDoiTac.SelectedValue)).ToList();
            }
        }
        void Reset()
        {
            idtam = 0;
            bttChinhSua.Enabled = false;
            bttXoa.Enabled = false;
        }
        void KiemTra()
        {
            if (idtam != 0)
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }
    }
}
